package com.roadmap.ajax.handler;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.directwebremoting.WebContextFactory;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

import com.roadmap.client.module.IManager;
import com.roadmap.client.module.plan.PlanManager;
import com.roadmap.client.module.statistic.IStatisticUtil;
import com.roadmap.client.module.statistic.ProgressPoint;
import com.roadmap.client.module.statistic.StatisticUtil;
import com.roadmap.common.RdcLogger;
import com.roadmap.common.RdcResourceBundle;
import com.roadmap.entities.LifePlan;
import com.roadmap.util.RdcEncryptor;
import com.roadmap.util.chart.IDataChart;
import com.roadmap.util.chart.RdcDataPieChart;
import com.roadmap.util.chart.RdcDataSeriesChart;

public class UtilHandler {
	private final static Logger logger = RdcLogger.getLogger(UtilHandler.class);

	private final IStatisticUtil _statisticUtil = new StatisticUtil();
	private final IDataChart _pieChart = new RdcDataPieChart();
	private final IDataChart _sezChart = new RdcDataSeriesChart();
	private final HttpSession _session = WebContextFactory.get().getSession();
	private IManager _planMgr = null;
	private final TimeSeriesCollection _seriesCollection = new TimeSeriesCollection();

	// decode the encrypted password
	public String decode(String encryptedStr) {
		if (encryptedStr == null)
			return null;
		return RdcEncryptor.getInstace().decode(encryptedStr);
	}

	/**
	 * this method generates a PNG of a chart and returns the file's name
	 */
	public String getChartOfAllGoals(String aspirantId, int width, int height) {
		if (aspirantId == null) {
			return null;
		}

		try {
			int[] counts = _statisticUtil.getGoalCounts(aspirantId);
			DefaultPieDataset data = new DefaultPieDataset();
			// the first data in counts is the amount of all goals, so it will
			// not shown in the chart
			for (int i = 1; i < RdcResourceBundle.LIFEGOAL_STATUS_DESCRIPTIONS.length
					&& i < counts.length; i++) {
				data.setValue(
						RdcResourceBundle.LIFEGOAL_STATUS_DESCRIPTIONS[i],
						counts[i]);
			}

			return _pieChart.convertDataSetToPNG(
					RdcResourceBundle.CHART_TITLE_FOR_ALL_GOALS, data, width,
					height, _session);
		}
		catch (Exception e) {

		}

		return null;
	}

	/**
	 * this method generates a PNG of a chart and returns the file's name
	 */
	public String getChartOfAllTasks(String aspirantId, int width, int height) {
		if (aspirantId == null) {
			return null;
		}

		try {
			int[] counts = _statisticUtil.getTaskCounts(aspirantId);
			DefaultPieDataset data = new DefaultPieDataset();
			// the first data in counts is the amount of all goals, so it will
			// not shown in the chart
			for (int i = 1; i < RdcResourceBundle.LIFETASK_STATUS_DESCRIPTIONS.length
					&& i < counts.length; i++) {
				data.setValue(
						RdcResourceBundle.LIFETASK_STATUS_DESCRIPTIONS[i],
						counts[i]);
			}

			return _pieChart.convertDataSetToPNG(
					RdcResourceBundle.CHART_TITLE_FOR_ALL_TASKS, data, width,
					height, _session);
		}
		catch (Exception e) {

		}

		return null;
	}

	/**
	 * this method generates a PNG of a chart and returns the file's name
	 */
	public String getChartOfPlanTasks(String planId, int width, int height) {
		if (planId == null) {
			return null;
		}

		try {
			int[] counts = _statisticUtil.getTaskCountsOfPlan(planId);
			DefaultPieDataset data = new DefaultPieDataset();
			// the first data in counts is the amount of all goals, so it will
			// not shown in the chart
			for (int i = 1; i < RdcResourceBundle.LIFETASK_STATUS_DESCRIPTIONS.length
					&& i < counts.length; i++) {
				data.setValue(
						RdcResourceBundle.LIFETASK_STATUS_DESCRIPTIONS[i],
						counts[i]);
			}

			return _pieChart.convertDataSetToPNG(
					RdcResourceBundle.CHART_TITLE_FOR_ALL_TASKS, data, width,
					height, _session);
		}
		catch (Exception e) {

		}

		return null;
	}

	private IManager getPlanManager(String aspirantId) {
		if (_planMgr == null) {
			_planMgr = new PlanManager(aspirantId);
		}
		return _planMgr;
	}

	public String getChartOfPlanProgress(String planId, int width, int height) {
		if (planId == null) {
			return null;
		}

		try {
			_seriesCollection.removeAllSeries();
			LifePlan plan = (LifePlan) getPlanManager(null).get(planId);
			List actualTread = _statisticUtil.getAcutalExecutingTrend(plan);
			List idealTread = _statisticUtil.getIdealExecutingTrend(plan);
			if (!actualTread.isEmpty()) {
				TimeSeries series = new TimeSeries(
						RdcResourceBundle.CHART_TITLE_FOR_PLAN_REAL_PROGRESS);
				Iterator iter = actualTread.iterator();
				while (iter.hasNext()) {
					ProgressPoint point = (ProgressPoint) iter.next();
					series.add(new Day(point.getXDate()), point
							.getYProportion());
				}
				_seriesCollection.addSeries(series);
			}

			if (!idealTread.isEmpty()) {
				TimeSeries series = new TimeSeries(
						RdcResourceBundle.CHART_TITLE_FOR_PLAN_IDEAL_PROGRESS);
				Iterator iter = idealTread.iterator();
				while (iter.hasNext()) {
					ProgressPoint point = (ProgressPoint) iter.next();
					series.add(new Day(point.getXDate()), point
							.getYProportion());
				}
				_seriesCollection.addSeries(series);
			}

			return _sezChart.convertDataSetToPNG(
					RdcResourceBundle.CHART_TITLE_FOR_PLAN_PROGRESS,
					_seriesCollection, width, height, _session);
		}
		catch (Exception e) {

		}
		return null;
	}
}
